package svcregistryserver

import (
	"context"

	"starsine.cn/appTpl/cmn"
	"starsine.cn/appTpl/cmn/logs"
	"starsine.cn/appTpl/svcregistry/grpc/handler"
	"starsine.cn/appTpl/svcregistry/grpc/svcregistryproto"
)

type RegistryProtoServer struct {
	svcregistryproto.UnimplementedRegistryProtoServer
}

func (p *RegistryProtoServer) SvcHeartbeat(ctx context.Context, in *svcregistryproto.HeartbeatReq) (*svcregistryproto.HeartbeatRes, error) {
	if handler.GetInstanceID() != in.InstanceID {
		logs.Warnf("receive error heartbeat, instanceID = %s", in.InstanceID)
		return &svcregistryproto.HeartbeatRes{
			Succ: false,
		}, nil
	}
	return &svcregistryproto.HeartbeatRes{
		Succ: true,
	}, nil
}

func (p *RegistryProtoServer) PubSvcRegister(ctx context.Context, in *svcregistryproto.PubSvcRegisterReq) (*svcregistryproto.Res, error) {
	logs.Debugf("receive pubsvcregister, param: %+v", in)

	cmn.GetEvntEmiter().Emit("svc-register", in)

	return &svcregistryproto.Res{
		Succ: true,
	}, nil
}

func (p *RegistryProtoServer) PubSvcUpdate(ctx context.Context, in *svcregistryproto.PubSvcUpdateReq) (*svcregistryproto.Res, error) {
	logs.Debugf("receive pubsvcupdate, param: %+v", in)

	cmn.GetEvntEmiter().Emit("svc-update", in)

	return &svcregistryproto.Res{
		Succ: true,
	}, nil
}
